package com.bt7274.controller;

import com.bt7274.grace.result.GraceJSONResult;
import com.bt7274.pojo.Forum;
import com.bt7274.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/forum")
public class ForumController {

    @Autowired
    private ForumService forumService;

    // 创建板块 传
    @PostMapping("/create")
    public GraceJSONResult createForum(@RequestBody Forum forum) {
        try {
            forumService.createForum(forum);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("创建失败: " + e.getMessage());
        }
    }

    // 删除板块
    @DeleteMapping("/delete/{id}")
    public GraceJSONResult deleteForum(@PathVariable String id) {
        try {
            forumService.deleteForum(id);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("删除失败: " + e.getMessage());
        }
    }

    // 重命名板块
    @PutMapping("/rename")
    public GraceJSONResult renameForum(@RequestParam String id, @RequestParam String newName) {
        try {
            forumService.renameForum(id, newName);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("重命名失败: " + e.getMessage());
        }
    }

    //修改板块描述
    @PutMapping("/reDescription")
    public GraceJSONResult reDescription(@RequestParam String id, @RequestParam String description) {
        try {
            forumService.reDescription(id, description);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("修改描述失败: " + e.getMessage());
        }
    }

    // 移动板块
    @PutMapping("/move")
    public GraceJSONResult moveForum(@RequestParam String id, @RequestParam String newParentId) {
        try {
            forumService.moveForum(id, newParentId);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("移动失败: " + e.getMessage());
        }
    }

    // 查询所有板块
    @GetMapping("/list")
    public GraceJSONResult listForums() {
        try {
            List<Forum> list = forumService.getAllForums();
            return GraceJSONResult.ok(list);
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("查询失败: " + e.getMessage());
        }
    }

    // 查询单个板块
    @GetMapping("/detail/{id}")
    public GraceJSONResult getForum(@PathVariable String id) {
        try {
            Forum forum = forumService.getForumById(id);
            return GraceJSONResult.ok(forum);
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("查询失败: " + e.getMessage());
        }
    }
}
